<template>
	<view class="select-address-list" v-show="show">
		<uni-popup ref="popupRef" :animation="false" type="bottom" border-radius="10px 10px 0 0" background-color="#fff">
			<view class="select-address-list-content">
				<view class="select-address-list-header">
					<view class="">
						选择地址
					</view>
					<view class="select-address-list-header-add" @click="addAddress">
						新增地址
					</view>
				</view>
				<scroll-view class="scroll-Y" scroll-y="true">

					<view :class="['select-address-item', {
							'select-address-item-active': currentAddress.userAddressId == item.userAddressId
						}]" v-for="item in addressList" :key="item.userAddressId" @click="() => addressSelect(item)">
						<view class="select-address-item-left">
							<view class="select-address-item-title">
								{{item.realName}} - {{item.mobile}}
							</view>
							<view class="select-address-item-desc">
								{{item.fullName}}{{item.address}}
							</view>
						</view>
						<view @click="editAddress(item)"
							style="width: 100rpx; height: 100rpx; text-align: center;line-height: 100rpx;">
							<u-icon name="edit-pen" size="30rpx" color="#999"></u-icon>
						</view>
					</view>

				</scroll-view>
				<view class="select-address-list-footer">
					<u-button size="medium" @click="close">取消</u-button>
					<u-button type="primary" @click="confirmAddress" size="medium">确定</u-button>
				</view>
			</view>
		</uni-popup>
		<SelectAddress ref="selectAddressRef" @delete="deleteAddress" @confirm="selectedAddress" />
	</view>
</template>

<script>
	import SelectAddress from '@/pages/index/components/select-address.vue'
	export default {
		data() {
			return {
				show: false,
				addressList: [],
				type: 1,
				currentAddress: {}
			}
		},
		components: {
			SelectAddress
		},
		methods: {
			openModal({
				type
			}) {
				this.type = type
				this.loadAddressList()
				this.show = true
				this.$refs.popupRef.open('bottom')
			},
			close() {
				this.show = false
				this.$refs.popupRef.close()
			},
			addressSelect(item) {
				this.currentAddress = this.currentAddress.userAddressId == item.userAddressId ? {} : item
			},
			// 查询地址列表
			loadAddressList() {
				const params = {
					page: 1,
					rows: 1000000
				}
				this.$u.api
					.postFindAddressList(params)
					.then(res => {
						this.addressList = res.data.rows;
						if (!this.currentAddress.mobile) {
							this.currentAddress = this.addressList.find((item) => item.isDefault == 1) || {}
						}
					})
					.catch(err => {
						console.log(err);
					});
			},
			editAddress(item) {
				this.$refs.selectAddressRef.openModal(item)
			},
			addAddress() {
				this.$refs.selectAddressRef.openModal({
					realName: '',
					mobile: '',
					city: '',
					province: '',
					county: '',
					address: '',
					fullName: '',
					isDefault: 0,
					userAddressId: '',
					provinceVal: 0,
					cityVal: 0,
					areaVal: 0
				})
			},
			selectedAddress() {
				this.loadAddressList()
			},
			confirmAddress() {
				this.$emit('confirm', {
					...this.currentAddress,
					type: this.type
				})
				this.close()
				this.currentAddress = {}
			},
			deleteAddress(userAddressId) {
				this.addressList = this.addressList.filter(item => item.userAddressId != userAddressId)
			}
		},
	}
</script>

<style lang="scss" scoped>
	.select-address-list-content {
		height: 75vh;
		display: flex;
		flex-direction: column;
	}

	.select-address-item {
		margin: 0 30rpx;
		padding-left: 20rpx;
		height: 150rpx;
		border-bottom: dashed 1rpx #e9e9e9;
		display: flex;
		justify-content: space-between;
		align-items: center;

		&-title {
			font-size: 36rpx;
			font-weight: 700;
		}

		&-desc {
			font-size: 28rpx;
			color: #515151;
		}

		&-active {
			border: solid 1rpx #3D9C9A;
			border-radius: 20rpx;

		}
	}

	.select-address-list-footer {
		width: 100%;
		display: flex;
		justify-content: space-around;
		height: 120rpx;
		align-items: center;
		background-color: #fff;
	}

	.scroll-Y {
		flex: 1
	}

	.select-address-list-header {
		display: flex;
		justify-content: space-between;
		font-size: 34rpx;
		padding: 20rpx;

		&-add {
			color: #3D9C9A;
		}
	}
</style>